package cn.com.cis.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.cis.domain.JobParams;
import cn.com.cis.domain.TemplateParamDef;
import cn.com.cis.domain.XmlJob;
import cn.com.cis.enums.MultipleThreadJobParams;
import cn.com.cis.enums.TemplateParamType;
import cn.com.cis.persistence.XmlJobMapper;
import cn.com.cis.plugins.mybatis.PageHelper;
import cn.com.cis.persistence.JobParamsMapper;
import cn.com.cis.persistence.TemplateParamDefMapper;

@Service
@Transactional
public class XmlJobService {

	@Autowired
	private XmlJobMapper xmlJobMapper;
	
	@Autowired
	private TemplateParamDefMapper paramDefMapper;
	
	@Autowired
	private JobParamsMapper jobParamsMapper;

	public void insertXmlJob(XmlJob job, List<TemplateParamDef> paramsList) {
		xmlJobMapper.insertXmlJob(job);

		int jobId = job.getXmlJobId();

		for (TemplateParamDef param : paramsList) {
			
			TemplateParamType paramType = TemplateParamType.fromHumanReadableName(param.getParamType());
			param.setParamType(paramType.getTypeName());
			
			param.setXmlJobId(jobId);
			paramDefMapper.insertParamDef(param);
		}
	}
	
	public void updateTemplate(XmlJob job, List<TemplateParamDef> paramsList) {
		xmlJobMapper.updateTemplate(job);

		int jobId = job.getXmlJobId();
		paramDefMapper.clearParamDefs(jobId);
		
		for (TemplateParamDef param : paramsList) {
			
			TemplateParamType paramType = TemplateParamType.fromHumanReadableName(param.getParamType());
			param.setParamType(paramType.getTypeName());
			
			param.setXmlJobId(jobId);
			paramDefMapper.insertParamDefWithId(param);
		}
	}

	public List<XmlJob> selectJobsAndTemps(int page, int pageSize) {
		PageHelper.startPage(page, pageSize);
		return xmlJobMapper.selectJobsAndTemps();
	}

	public List<TemplateParamDef> selectParamDefsByTemplateId(int templateId) {
		return paramDefMapper.selectParamDefsByTemplateId(templateId);
	}

	public TemplateParamDef selectParamDefByParamId(int paramId) {
		return paramDefMapper.selectParamDefByParamId(paramId);
	}
	
	public JobParams selectJobParamByParamId(int paramId) {
		return jobParamsMapper.selectJobParamByParamId(paramId);
	}

	public void insertJobWithParams(XmlJob job, List<JobParams> params) {
		xmlJobMapper.insertXmlJob(job);
		int jobId = job.getXmlJobId();
		
		for(JobParams param : params){
			param.setXmlJobId(jobId);
			jobParamsMapper.insertJobParam(param);
		}
	}

	public List<XmlJob> selectJobs() {
		return xmlJobMapper.selectJobs();
	}

	public void deleteJobOrTemp(int id) {
		xmlJobMapper.deleteJobOrTemp(id);
	}
	
	public XmlJob selectJobById(int id) {
		return xmlJobMapper.selectJobById(id);
	}
	
	public List<JobParams> selectParamsOfJob(int id) {
		List<JobParams> jobParams = jobParamsMapper.selectParamsOfJob(id);
		
		for(JobParams jobParam : jobParams){
			int paramDefId = jobParam.getParamDefId();
			TemplateParamDef paramDef = paramDefMapper.selectParamDefByParamId(paramDefId);
			if(null != paramDef){
				jobParam.setParamName(paramDef.getParamName());
				jobParam.setParamType(paramDef.getParamType());
			}
		}
		
		return jobParams;
	}

	public void updateSimpleJobWithParams(XmlJob job, List<JobParams> params) {
		xmlJobMapper.updateSimpleJob(job);
		
		jobParamsMapper.clearAllParamsOfJob(job.getXmlJobId());
		for(JobParams param : params){
			param.setXmlJobId(job.getXmlJobId());
			jobParamsMapper.insertJobParam(param);
		}
	}

	public void updateStandardJobWithParams(XmlJob job, List<JobParams> params) {
		xmlJobMapper.updateStandardJob(job);
		
		jobParamsMapper.clearAllParamsOfJob(job.getXmlJobId());
		for(JobParams param : params){
			param.setXmlJobId(job.getXmlJobId());
			jobParamsMapper.insertJobParam(param);
		}
		
	}

	public TemplateParamDef selectParamDefByParamName(int createFromTemplate, String param) {
		return paramDefMapper.selectParamDefByParamName(createFromTemplate, param);
	}

	public List<TemplateParamDef> selectNonThreadParamDefsByTemplateId(int tempId) {
		return paramDefMapper.selectNonThreadParamDefsByTemplateId(tempId);
	}

	public JobParams selectParamByParamName(int xmlJobId, String param) {
		return jobParamsMapper.selectParamByParamName(xmlJobId, param);
	}

	public JobParams selectJobParamByParamDefId(int parseInt) {
		return jobParamsMapper.selectJobParamByParamDefId(parseInt);
	}
	
	public boolean isJobMultipleThread(int xmlJobId) {
		if("on".equals(xmlJobMapper.selectParamValueByParamName(xmlJobId, MultipleThreadJobParams.IS_SPLIT + ""))){
			return true;
		}
		
		return false;
	}
	
	public Date selectStartDate(int xmlJobId) throws ParseException {
		String startDate = xmlJobMapper.selectParamValueByParamName(xmlJobId, MultipleThreadJobParams.START_DATE + "");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=sdf.parse(startDate); 
		return date;
	}
	
	public Date selectEndDate(int xmlJobId) throws ParseException {
		String startDate = xmlJobMapper.selectParamValueByParamName(xmlJobId, MultipleThreadJobParams.END_DATE + "");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=sdf.parse(startDate); 
		return date;
	}
	
	public Integer selectThreadNumber(int xmlJobId) {
		String threadNumber = xmlJobMapper.selectParamValueByParamName(xmlJobId, MultipleThreadJobParams.THREAD_NUMBER + "");
		return Integer.parseInt(threadNumber);
	}
	
	public Integer selectTaskLoad(int xmlJobId) {
		String taskLoad = xmlJobMapper.selectParamValueByParamName(xmlJobId, MultipleThreadJobParams.TASK_LOAD + "");
		return Integer.parseInt(taskLoad);
	}

}
